package com.kuaimacode.kframework.api.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.mybatis.mapper.score.SysAdminScoreManagentmentMapper;
import com.kuaimacode.kframework.mybatis.models.score.AdminScoreManagentment;
import io.swagger.models.auth.In;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author Administrator
 * @date 2019/04/08
 */
@Service
public class ScoreManagentmentService extends ServiceImpl<SysAdminScoreManagentmentMapper, AdminScoreManagentment> {

    	/**
    	 *  分页查找所有的数据
    	 * @param pageNum 当前页码，从1开始
    	 * @param pageSize  一页显示多少行
    	 * @return
    	 */
    	public PageInfo<AdminScoreManagentment> findAll(int pageNum, int pageSize) {
    		PageHelper.startPage(pageNum, pageSize);
    		List<AdminScoreManagentment> datas = baseMapper.selectList(null);
    		return new PageInfo<AdminScoreManagentment>(datas);
    	}

	public AdminScoreManagentment findOne() {
    		QueryWrapper<AdminScoreManagentment> queryWrapper = new QueryWrapper<>();
    		queryWrapper.eq("status","1");
		List<AdminScoreManagentment> datas = baseMapper.selectList(queryWrapper);
		if (CollectionUtils.isNotEmpty(datas)) {
			return datas.get(0);
		}
		return new AdminScoreManagentment();
	}

	/**
	 * 评分
	 * @param agentId 分站
	 * @param orderNum 每周订单数量
	 * @param good 好评/差评
	 * @param weater 恶劣天气服务
	 * @param inNight 深夜服务
	 * @param complaint 被投诉
	 * @param inTtime 超时
	 * @Param Refused 拒绝
	 * @return
	 */
    	public Long computingScore(Long agentId,Integer orderNum,Boolean good,Boolean weater,Boolean inNight
				,Boolean complaint,Boolean inTtime,Boolean Refused,Boolean orderConfrim){
    		QueryWrapper<AdminScoreManagentment> qr = new QueryWrapper<>();
			qr.eq("status","1");
    		List<AdminScoreManagentment> scores = baseMapper.selectList(qr);
			Long score = 0l;

			if (CollectionUtils.isNotEmpty(scores)){
				AdminScoreManagentment managentment = scores.get(0);
				if (managentment !=null){
					//完成订单
					if (orderConfrim){
						if (managentment.getCompleteOrderScore()!=null
								&&managentment.getCompleteOrderScore()!=0){
							score +=managentment.getCompleteOrderScore();
						}else{
							score +=10;
						}
					}
					//特殊天气
					if (weater){
						score +=managentment.getSpecialWeatherScore();
					}
					//深夜
					if (inNight){
						score +=managentment.getNightScore();
					}
					//订单数
					if (orderNum!=null){
						if (orderNum>=Integer.
								parseInt(String.valueOf(managentment.getWeekGoalNum()==null?0:managentment.getWeekGoalNum()))){
							score+=managentment.getWeekGoalScore()==null?0L:managentment.getWeekGoalScore();
						}
					}

					//好评加分
					if (good){
						score+=managentment.getGoodScore();
					}
					//拒绝系统派单
					if (Refused){
						score+=-(managentment.getSysOrderRefuseScore()==null?0l:managentment.getSysOrderRefuseScore());
					}
					//超时
					if (inTtime){
						score+=-(managentment.getTimeoutTenScore()==null?0l:managentment.getTimeoutTenScore());
					}
					//投诉
					if (complaint){
						score+=-(managentment.getComplainRationalScore()==null?0l:managentment.getComplainRationalScore());

					}
				}
			}else{
				//默认分值
				score = 10l;
			}
			return score;
		}

	/**
	 * 计算星级
	 */
	public String parseStar(Long score){
		AdminScoreManagentment managentment = findOne();
		if (managentment.getOneStarScoreBegin()!=null){
			if (score<managentment.getOneStarScoreBegin()){
				return "0.5";
			}
		}
		if (managentment.getOneStarScoreBegin()!=null&&managentment.getOneStarScoreEnd()!=null){
			if (score<=managentment.getOneStarScoreEnd()&&score>=managentment.getOneStarScoreBegin()){
				return "1";
			}
		}
		if (managentment.getTwoStarScoreBegin()!=null&&managentment.getTwoStarScoreEnd()!=null){
			if (score<=managentment.getTwoStarScoreEnd()&&score>=managentment.getTwoStarScoreBegin()){
				return "2";
			}
		}
		if (managentment.getTrdStarScoreBegin()!=null&&managentment.getTrdStarScoreEnd()!=null){
			if (score<=managentment.getTrdStarScoreEnd()&&score>=managentment.getTrdStarScoreBegin()){
				return "3";
			}
		}
		if (managentment.getForStarScoreBegin()!=null&&managentment.getForStarScoreEnd()!=null){
			if (score<=managentment.getForStarScoreEnd()&&score>=managentment.getForStarScoreBegin()){
				return "4";
			}
		}
		if (managentment.getFveStarScoreBegin()!=null&&managentment.getFveStarScoreEnd()!=null){
			if (score>=managentment.getFveStarScoreBegin()){
				return "5";
			}
		}
		return "";

	}
}

